<%--
  @author Duminda Dharmakeerthi
  @author Supun Nimesh Karunathilaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/dateSelector.js"></script>
<script src="${pageContext.request.contextPath}/js/landRegistration/longTermLease.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {
        validateLongTermLeaseDetails();

    });

    function validateLongTermLeaseDetails() {
        $('#issueDate').rules("add", {
            isDateFormated:true,
            isFeatureDate:true,
            messages:{
                ageValidate:$('#pls_enter_valid_age').val()}
        });

        $('#folioDate').rules("add", {
            isDateFormated:true,
            isFeatureDate:true,
            messages:{
                ageValidate:$('#pls_enter_valid_age').val()}
        });
        $('#startedDate').rules("add", {
            isDateFormated:true,
            isFeatureDate:true,
            messages:{
                ageValidate:$('#pls_enter_valid_age').val()}
        });
    }


    $(function () {
        if ($('#landMode').val() != 'DETAIL') {
            var disNamePrfix = getDistrictPrefix($('#districtId').val());
            $('#leaseDiagramNo').val('L/' + disNamePrfix + '/');
        }


    });

    function leaseDiagramNoValidate() {
        var leaseDiagramNo = $('#leaseDiagramNo').val();
        var mySplitResult = leaseDiagramNo.split("/");
        var result1 = false;
        var result2 = false;
        var result3 = false;
        var resultValidate = false;
        if (mySplitResult[0] == 'L') {
            result1 = true;
        } else {
            result1 = false;
        }
        var disPrifix = getDistrictPrefix($('#districtId').val());
        if (mySplitResult[1] == disPrifix) {
            result2 = true;
        } else {
            result2 = false;
        }
        if (mySplitResult[2].length == 0) {
            var result3 = false;
        } else {
            var result3 = true;
        }
        ;
        if (result1 == true && result2 == true && result3 == true) {
            resultValidate = true;
        } else {
            resultValidate = false;
        }
        //alert(resultValidate);
        jQuery.validator.addMethod("leaseDiagramNoValidate", function(element) {
            var result = true;

            if (resultValidate) {
                result = true;
            }
            else {
                result = false;
            }  //alert(result);
            return  result;
        }, "leaseDiagramNo ");
    }

    function leaseDiagramNoValidateDisplay() {
        leaseDiagramNoValidate();
        $("#gov-land-init-info-form").validate({
            rules:{
                leaseDiagramNo:{
                    leaseDiagramNoValidate: true
                }
            },
            messages:{
                leaseDiagramNo:{
                    leaseDiagramNoValidate: "Please enter a Valid Lease Diagram Number "
                }
            }
        });
    }

    function getDistrictPrefix(dis) {
        var disName = dis.toLowerCase();
        if (disName == 'colombo') {
            disName = 'CO';
        } else if (disName == 'gampaha') {
            disName = 'GAM';
        } else if (disName == 'kaluthara') {
            disName = 'KALU';
        }
        return disName;
    }

    function initialValidate() {
        initialLongTermLease();
    }
    function detailValidate() {
        //        alert("detail");
    }
    function showViolationComment() {
        $('#violationComment').show();
    }

    function hideViolationComment() {
        $('#violationComment').hide();
        $('#conditionsViolationComment').val('');
    }

    function violationCommentClick() {
        if ($('#conditionsViolatedOrNot0').is(':checked')) {
            hideViolationComment();
        } else {
            showViolationComment();
        }
    }
</script>
<SCRIPT language="javascript">
    function disableFields() {
        var fields = new Array(
                'referenceNo',
                'issueDate',
                'folioNo',
                'folioDate',
                'folioLocation',
                'leasePeriod',
                'leaseDiagramNo',
                'permittedLandUsageId',
                'currentLandUsageId',
                'developed-value',
                'commercial-value',
                'undeveloped-value',
                'leaseArrearsFromYear',
                'leaseArrearsToYear',
                'amend-period',
                'conditionsViolatedOrNot0',
                'conditionsViolatedOrNot1',
                'conditionsViolationComment',
                'yes',
                'no',
                'leasePeriodWord'
                );
        enableFields(fields);
    }

    function disableMortgage() {
        var fields = new Array(
                'nameOfBank',
                'addressOfBank',
                'startedDate',
                'mortgagePeriodInYears',
                'mortgagePeriodInMonths',
                'conditions',
                'powerOfAttorney'
                );
        enableFields(fields);
    }

    function disableInstitute() {
        var fields = new Array(
                'name',
                'address',
                'nic',
                'mobile',
                'landPhone',
                'email',
                'fax'
                );
        enableFields(fields);
    }

    $(function () {
        $('#nominatedSuccessor').hide();

        disableFields();
        disableMortgage();
        disableInstitute();

        if ($('#landMode').val() == 'DETAIL') {
            violationCommentClick();
        } else {
            hideViolationComment();
        }
    });

    function showAmend() {
        $('#amend-period').show();
        $('.amend').show();
    }
    function hideAmend() {
        $('#amend-period').hide();
        $('.amend').hide();
    }

    $(function() {
        var holderPersonHtml = $('#holderPerson').html();
        var holderInstituteHtml = $('#holderInstitute').html();
        if ($('#landMode').val() == '') {
            $('#clientType1').attr('checked', true);
        }
        $('#clientType0').bind('click', function() {
            $('#holderPerson').html(' ');
            $('#holderInstitute').html(holderInstituteHtml);
        });
        $('#clientType1').bind('click', function() {
            $('#holderInstitute').html(' ');
            $('#holderPerson').html(holderPersonHtml);
        });
        if ($('#clientType0').is(':checked')) {
            $('#holderPerson').html(' ');
            $('#holderInstitute').html(holderInstituteHtml);
        } else if ($('#clientType1').is(':checked')) {
            $('#holderInstitute').html(' ');
            $('#holderPerson').html(holderPersonHtml);
        }

        if ($('#landMode').val() == 'DETAIL') {
            enableFields(new Array('clientType0', 'clientType1'));
        }
    });
</SCRIPT>

<h3><s:label value="%{getText('long_term_lease_details.label')}"/></h3>
<fieldset>
<div id="long-term-lease-details">
    <table class="width-100">
        <tr>
            <td><s:label value="%{getText('lease_no.label')}"/></td>
            <td><s:textfield id="referenceNo" name="alienation.referenceNo" cssClass="width-200-px"/></td>
            <td><s:label value="%{getText('date_of_approved.label')}"/> <s:label
                    value="%{getText('date_format.label')}" cssClass="date-format"/></td>
            <td><s:textfield id="issueDate" name="alienation.issueDate" onclick="selectDate('issueDate')"
                             cssClass="width-200-px" onKeyPress="return dateNumbersOnly(event,true)"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('folio_no.label')}"/></td>
            <td><s:textfield id="folioNo" name="alienation.folioNumber" cssClass="width-200-px"/></td>
            <td><s:label value="%{getText('folio_date.label')}"/> <s:label value="%{getText('date_format.label')}"
                                                                           cssClass="date-format"/></td>
            <td><s:textfield id="folioDate" name="alienation.folioDate" onclick="selectDate('folioDate')"
                             cssClass="width-200-px" onKeyPress="return dateNumbersOnly(event,true)"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('folio_location.label')}"/></td>
            <td colspan="3"><s:textarea id="folioLocation" name="alienation.folioLocation" rows="1"
                                        cssClass="width-595-px"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('lease_period.label')}"/></td>
            <td><s:textfield id="leasePeriod" name="alienation.leaseDetail.leasePeriod"
                             cssClass="width-200-px" onKeyPress="return dateNumbersOnly(event,true)"/></td>
        </tr>
        <tr>
            <td><s:label value="Lease Period in word"/></td>
            <td><s:textarea id="leasePeriodWord"
                                        name="alienation.leaseDetail.leasePeriodInWord"
                                        cssClass="width-200-px" rows="2"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('lease_diagram_no.label')}"/></td>
            <td colspan="3"><s:textfield id="leaseDiagramNo" cssClass="width-200-px"
                                         name="alienation.leaseDetail.leaseDiagramNo"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('purpose_of_lease.label')}"/></td>
            <td colspan="3"><s:select id="permittedLandUsageId" name="alienation.permittedUsage.id"
                                      list="landUsageList" cssClass="width-200-px"
                                      headerKey="0"
                                      headerValue="%{getText('select.label')}"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('present_usage.label')}"/></td>
            <td colspan="3"><s:select id="currentLandUsageId" name="alienation.currentLandUsage.id"
                                      list="landUsageList" headerKey="0" cssClass="width-200-px"
                                      headerValue="%{getText('select.label')}"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('valuation_of_land.label')}"/></td>
            <td><s:label value="%{getText('developed_value.label')}"/> <s:label
                    value="%{getText('rs.label')}"/></td>
            <td colspan="2"><s:textfield id="developed-value" cssClass="width-200-px"
                                         name="alienation.leaseDetail.developedValue"
                                         onKeyPress="return dateNumbersOnly(event,true)"/></td>
        </tr>
        <tr>
            <td rowspan="2"></td>
            <td><s:label value="%{getText('commercial_value.label')}"/> <s:label
                    value="%{getText('rs.label')}"/></td>
            <td colspan="2"><s:textfield id="commercial-value" cssClass="width-200-px"
                                         name="alienation.leaseDetail.commercialValue"
                                         onKeyPress="return dateNumbersOnly(event,true)"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('undeveloped_value.label')}"/> <s:label
                    value="%{getText('rs.label')}"/></td>
            <td colspan="2"><s:textfield id="undeveloped-value" cssClass="width-200-px"
                                         name="alienation.leaseDetail.unDevelopedValue"
                                         onKeyPress="return dateNumbersOnly(event,true)"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('lease_arrears.label')}"/></td>
            <td colspan="3">
                <s:label value="%{getText('from_year.label')}"/>
                <s:textfield id="leaseArrearsFromYear" name="alienation.leaseDetail.leaseArrearsFromYear"
                             onKeyPress="return dateNumbersOnly(event,true)" cssStyle="width: 80px;"/>
                <s:label value="%{getText('to_year.label')}"/>
                <s:textfield id="leaseArrearsToYear" name="alienation.leaseDetail.leaseArrearsToYear"
                             onKeyPress="return dateNumbersOnly(event,true)" cssStyle="width: 80px;"/>
            </td>
        </tr>
        <tr>
            <td><s:label value="%{getText('lease_revised.label')}"/> ?</td>
            <td><s:label value="%{getText('yes.label')}"/> <input id="yes" type="radio" name="amend"
                                                                  checked="checked"
                                                                  value="1"
                                                                  onclick="showAmend()"/> <s:label
                    value="%{getText('no.label')}"/> <input id="no" type="radio" name="amend" value="0"
                                                            onclick="hideAmend()"/>
            </td>
            <td><s:label cssClass="amend" value="%{getText('rivision_interval.label')}"/></td>
            <td><s:textfield cssClass="amend width-200-px" id="amend-period"
                             name="alienation.leaseDetail.leaseAmendPeriod"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('conditionsViolatedOrNot.label')}"/></td>
            <td colspan="2"><s:radio id="conditionsViolatedOrNot" name="alienation.conditionsViolatedOrNot"
                                     list="#@java.util.HashMap@{'1':getText('yes.label'),'0':getText('no.label')}"
                                     onclick="violationCommentClick()"/></td>
        </tr>
        <tr id="violationComment">
            <td><s:label value="%{getText('conditionsViolationComment.label')}"/></td>
            <td colspan="3"><s:textarea id="conditionsViolationComment" name="alienation.conditionsViolationComment"
                                        rows="1"
                                        cssClass="width-595-px"/></td>
        </tr>
    </table>
</div>
</fieldset>

<h3><s:label value="%{getText('mortgage_details.label')}"/></h3>
<fieldset>
<div id="mortgage-details">
    <table class="width-100">
        <tr>
            <td><s:label value="%{getText('name_of_bank_institute.label')}"/></td>
            <td colspan="3"><s:textarea id="nameOfBank" name="alienation.mortgage.nameOfBank" rows="1"
                                        cssClass="width-595-px"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('address_of_bank_institute.label')}"/></td>
            <td colspan="3"><s:textarea id="addressOfBank" name="alienation.mortgage.addressOfBank" rows="1"
                                        cssClass="width-595-px"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('mortgage_date.label')}"/> <s:label
                    value="%{getText('date_format.label')}" cssClass="date-format"/></td>
            <td colspan="3"><s:textfield id="startedDate" name="alienation.mortgage.startedDate"
                                         onclick="selectDate('startedDate')" cssClass="width-200-px"
                                         onKeyPress="return dateNumbersOnly(event,true)"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('mortgage_period.label')}"/></td>
            <td><s:label value="%{getText('years.label')}"/> <s:textfield id="mortgagePeriodInYears"
                                                                          name="alienation.mortgage.mortgagePeriodInYears"
                                                                          onKeyPress="return dateNumbersOnly(event,true)"
                                                                          cssStyle="width: 80px;"/></td>
            <td><s:label value="%{getText('months.label')}"/> <s:textfield id="mortgagePeriodInMonths"
                                                                           name="alienation.mortgage.mortgagePeriodInMonths"
                                                                           onKeyPress="return dateNumbersOnly(event,true)"
                                                                           cssStyle="width: 80px;"/></td>
            <td></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('mortgage_conditions.label')}"/></td>
            <td colspan="3"><s:textarea id="conditions" name="alienation.mortgage.conditions" rows="1"
                                        cssClass="width-595-px"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('power_of_attorney.label')}"/></td>
            <td colspan="3"><s:textarea id="powerOfAttorney" name="alienation.mortgage.powerOfAttorney" rows="1"
                                        cssClass="width-595-px"/></td>
        </tr>
    </table>
</div>
</fieldset>

<h3><s:label value="%{getText('holder_details.label')}"/> </h3>
<fieldset>
<div id="personal-details">
    <table class="width-100">
        <tr>
            <td align="center" colspan="4">
                <s:radio id="clientType" name="alienation.holder.holderType"
                         list="#@java.util.HashMap@{'1':getText('person.label'),'0':getText('institute.label')}"/>
                <s:if test="land.mode.ordinal() == 1">
                    <s:hidden name="alienation.holder.holderType"/>
                </s:if>
            </td>
        </tr>
    </table>
    <table id="holderInstitute" class="width-100">
        <tr>
            <td><s:label value="%{getText('name_of_institute.label')}"/></td>
            <td colspan="3"><s:textarea id="name" name="alienation.holder.name" cssClass="width-595-px"
                                        rows="1"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('address.label')}"/></td>
            <td colspan="3"><s:textarea id="address" name="alienation.holder.contacts.address"
                                        cssClass="width-595-px"
                                        rows="1"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('registration_no.label')}"/></td>
            <td colspan="3"><s:textfield id="nic" name="alienation.holder.registrationNumberOrNIC"
                                         cssClass="width-200-px"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('contact_number.label')}"/></td>
            <td><s:label value="%{getText('mobile.label')}"/></td>
            <td><s:textfield id="mobile" name="alienation.holder.contacts.mobile" cssClass="width-200-px"/></td>
        </tr>
        <tr>
            <td></td>
            <td><s:label value="%{getText('land_line.label')}"/></td>
            <td><s:textfield id="landPhone" name="alienation.holder.contacts.landPhone"
                             cssClass="width-200-px"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('email.label')}"/></td>
            <td colspan="3"><s:textfield id="email" name="alienation.holder.contacts.email"
                                         cssClass="width-200-px"/></td>
        </tr>
        <tr>
            <td><s:label value="%{getText('fax.label')}"/></td>
            <td colspan="3"><s:textfield id="fax" name="alienation.holder.contacts.fax"
                                         cssClass="width-200-px"/></td>
        </tr>
    </table>
    <table id="holderPerson" class="width-100">
        <s:include value="personalDetailsPrintReport.jsp"/>
    </table>
</div>
</fieldset>

<h3><s:label value="%{getText('occupier_details.label')}"/></h3>
<fieldset>
<div id="occupier-details">
    <table class="width-100">
        <s:include value="encroacherDetailsPrintReport.jsp"/>
    </table>
</div>
</fieldset>


<s:hidden name="alienation.id"/>
<s:hidden name="alienation.holder.id"/>
<s:hidden name="alienation.holder.contacts.id"/>
<s:hidden name="alienation.leaseDetail.id"/>
<s:hidden name="alienation.mortgage.id"/>


<s:hidden id="pls_enter_reference_no" value="%{getText('pls_enter_reference_no')}"/>
<s:hidden id="pls_enter_name_of_the_land" value="%{getText('pls_enter_name_of_the_land')}"/>
<s:hidden id="pls_enter_plan_no" value="%{getText('pls_enter_plan_no')}"/>
<s:hidden id="pls_enter_lot_no" value="%{getText('pls_enter_lot_no')}"/>
<s:hidden id="pls_enter_valid_no" value="%{getText('pls_enter_valid_no')}"/>
<s:hidden id="pls_select_gn_division" value="%{getText('pls_select_gn_division')}"/>